package com.example.rabbitmq.starter.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

/**
 * 业务异常类
 * 用于处理业务逻辑中的异常情况
 *
 * @author example
 * @since 1.0.0
 */
@Getter
public class BusinessException extends RuntimeException {
    
    /**
     * 错误码
     */
    private final Integer code;
    
    /**
     * HTTP状态码
     */
    private final HttpStatus httpStatus;

    public BusinessException(String message) {
        super(message);
        this.code = HttpStatus.BAD_REQUEST.value();
        this.httpStatus = HttpStatus.BAD_REQUEST;
    }

    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
        this.httpStatus = HttpStatus.BAD_REQUEST;
    }

    public BusinessException(Integer code, String message, HttpStatus httpStatus) {
        super(message);
        this.code = code;
        this.httpStatus = httpStatus;
    }

    public BusinessException(String message, Throwable cause) {
        super(message, cause);
        this.code = HttpStatus.BAD_REQUEST.value();
        this.httpStatus = HttpStatus.BAD_REQUEST;
    }

    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.httpStatus = HttpStatus.BAD_REQUEST;
    }

    public BusinessException(Integer code, String message, HttpStatus httpStatus, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.httpStatus = httpStatus;
    }

    /**
     * 创建参数错误异常
     */
    public static BusinessException paramError(String message) {
        return new BusinessException(HttpStatus.BAD_REQUEST.value(), message, HttpStatus.BAD_REQUEST);
    }

    /**
     * 创建资源未找到异常
     */
    public static BusinessException notFound(String message) {
        return new BusinessException(HttpStatus.NOT_FOUND.value(), message, HttpStatus.NOT_FOUND);
    }

    /**
     * 创建权限不足异常
     */
    public static BusinessException forbidden(String message) {
        return new BusinessException(HttpStatus.FORBIDDEN.value(), message, HttpStatus.FORBIDDEN);
    }

    /**
     * 创建未授权异常
     */
    public static BusinessException unauthorized(String message) {
        return new BusinessException(HttpStatus.UNAUTHORIZED.value(), message, HttpStatus.UNAUTHORIZED);
    }

    /**
     * 创建服务器内部错误异常
     */
    public static BusinessException internalError(String message) {
        return new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 创建冲突异常
     */
    public static BusinessException conflict(String message) {
        return new BusinessException(HttpStatus.CONFLICT.value(), message, HttpStatus.CONFLICT);
    }
}